package com.qf.controller;


import com.qf.entity.Goods;
import com.qf.entity.R;
import com.qf.service.GoodsService;
import com.qf.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Goods)表控制层
 *
 * @author makejava
 * @since 2020-12-04 09:43:15
 */
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    /**
     * 服务对象
     */
    @Autowired
    private GoodsService goodsService;

    /**
     * 请求秒杀场次的时间列表
     * @return
     */
    @RequestMapping("/times")
    public R<List<String>> getTimes(){
        List<String> times = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            String time = DateUtil.getTimeByIndex(i);
            times.add(time);
        }
        return new R<List<String>>().setData(times);
    }

    /**
     * 根据时间场次查询商品列表
     * @return
     */
    @RequestMapping("/getGoodsByTime")
    public R getGoodsByTime(String time){
        List<Goods> goods = goodsService.queryByTime(time);
        return new R().setData(goods);
    }

    /**
     * 根据id查询商品信息
     * @param gid
     * @return
     */
    @RequestMapping("/getGoodById")
    public R<Goods> getGoodById(Integer gid) {
        Goods goods = goodsService.getById(gid);
        return new R<Goods>().setData(goods);
    }

    /**
     * 获取服务器的当前时间
     * @return
     */
    @RequestMapping("/now")
    public R getTimeNow(){
        return new R().setData(new Date());
    }
}